const mongoose = require('mongoose');
module.exports={
    async changePassword(options){
        const EncryptedNotepadModel=mongoose.models['EncryptedNotepad'];
        let userId=options.self._id.toString();
        let en=await EncryptedNotepadModel.findOne({userId:userId});
        if(!!en){
            en.password=options.password;
            await en.save();
        }
    },
    async checkPassword(options){
        const EncryptedNotepadModel=mongoose.models['EncryptedNotepad']
        let userId=options.self._id.toString();
        let en=await EncryptedNotepadModel.findOne({userId:userId});
        if(!en){
            en=await EncryptedNotepadModel.create({
                userId:userId,
                password:options.password,
                editTimeTick:new Date().getTime()
            });
        }else{
            en.password=options.password;
        }
        await en.save();
        return {content:en.content};
    },
    async saveContent(options){
        const EncryptedNotepadModel=mongoose.models['EncryptedNotepad'];
        let userId=options.self._id.toString();
        let en=await EncryptedNotepadModel.findOne({userId:userId});
        if(!!en) {
            en.content = options.content;
            en.editTimeTick = new Date().getTime();
            await en.save();
        }
    }

}